//
//  UIButton.cpp
//  storm
//
//  Created by wangdi on 13-8-3.
//
//

#include "UIButton.h"

using namespace cocos2d;

namespace storm
{
    
    //////////////////////////////////////////////////////////////////////////
    //
    // 虚函数
    //
    //////////////////////////////////////////////////////////////////////////
    
    UIWidget *UIButton::onReceiveNewTouch(CCTouch *pTouch)
    {
        if(!isContainsTouch(pTouch))
        {
            return NULL;
        }
        // 获取到一个新的触摸信息
        
        SetState(STATE_CLICKING);
        
        return this;
        
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIButton::onCurTouchMoveOut(cocos2d::CCTouch *pTouch)
    {
        // 获取到当前触摸移除BUTTON区域
        loseFocus();
        
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIButton::onCurTouchFinish(cocos2d::CCTouch *pTouch)
    {
        // 获取当前触摸取消
        
        SetState(STATE_CLICKED);
        UIWidget::loseFocus();
        OnClick.emit(this);   
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIButton::loseFocus()
    {
        SetState(STATE_NORMAL);
        UIWidget::loseFocus();
    }
    
    //////////////////////////////////////////////////////////////////////////
    //
    // 静态函数
    //
    //////////////////////////////////////////////////////////////////////////
    
    UIButton *UIButton::Create(const char *szNormalName, bool bUseFrame, CCSize size)
    {
        UIButton *pNewButton = UIButton::create();
        if(NULL != szNormalName)
        {
            pNewButton->SetNormalSprite(szNormalName, bUseFrame);
        }
        
        if(CCSizeZero.equals(size))
        {
            CCSprite *pSpr = pNewButton->NormalSprite();
            if(NULL != pSpr) pNewButton->setContentSize(pSpr->getContentSize());
        }
        else
        {
            pNewButton->setContentSize(size);
        }
       
        return pNewButton;
    }
    
    //////////////////////////////////////////////////////////////////////////
    //
    // 公有函数
    //
    //////////////////////////////////////////////////////////////////////////
    
    UIButton::UIButton()
    : m_pSprClicked(NULL)
    , m_pSprClicking(NULL)
    , m_pSprNormal(NULL)
    , m_pFntNormal(NULL)
    , m_pFntReplace(NULL)
    , m_scaleFactor(1.1f)
    , m_scaleNormal(1.0f)
    , m_opacityFactor(128)
    {
    }
    
    
//    //------------------------------------------------------------------------------------------------------------------------------------/
//    void UIButton::CreateLable(const char *szFontFile, cocos2d::CCTextAlignment alignment, float scale, const char *szNormal, const char *szReplace)
//    {
//        if(NULL == m_pFntNormal && NULL != szNormal && NULL != m_pSprNormal)
//        {
//            m_pFntNormal = CCLabelBMFont::create(szNormal, szFontFile);
//            m_pFntNormal->setAlignment(alignment);
//            m_pFntNormal->setPosition(ccp(m_pSprNormal->getContentSize().width*0.5f, m_pSprNormal->getContentSize().height*0.5f));
//            m_pSprNormal->addChild(m_pFntNormal);
//            m_pFntNormal->setScale(scale);
//            AddTransformNode(m_pFntNormal);
//        }
//        
//        if(NULL == m_pFntReplace && NULL != szReplace && NULL != m_pSprNormal)
//        {
//            m_pFntReplace = CCLabelBMFont::create(szReplace, szFontFile);
//            m_pFntReplace->setAlignment(alignment);
//            m_pFntReplace->setPosition(ccp(m_pSprReplace->getContentSize().width*0.5f, m_pSprReplace->getContentSize().height*0.5f));
//            m_pSprReplace->addChild(m_pFntReplace);
//            m_pFntReplace->setScale(scale);
//            AddTransformNode(m_pFntReplace);
//        }
//    }
    
    void UIButton::SetNormalSprite(const char *szName, bool bUseFrame)
    {
        SetSprite(m_pSprNormal, szName, bUseFrame);
    }
    
    void UIButton::SetClickingSprite(const char *szName, bool bUseFrame)
    {
        SetSprite(m_pSprClicking, szName, bUseFrame);
        m_pSprClicking->setVisible(false);
    }
    
    void UIButton::SetClickedSprite(const char *szName, bool bUseFramee)
    {
        SetSprite(m_pSprClicked, szName, bUseFramee);
        m_pSprClicked->setVisible(false);
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIButton::SetScaleFactor(float scale)
    {
        if(m_pSprNormal  != NULL)
        {
            if(m_pSprNormal->getScale() == m_scaleFactor)
            {
                m_pSprNormal->setScale(scale);
            }
        }
        m_scaleFactor = scale;
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIButton::SetOpacityFactor(float opacity)
    {
        if(m_pSprNormal  != NULL)
        {
            if(m_pSprNormal->getOpacity() == m_opacityFactor)
            {
                m_pSprNormal->setScale(opacity);
            }
        }
        m_opacityFactor = opacity;
    }
    
    //------------------------------------------------------------------------------------------------------------------------------------/
    void UIButton::SetScaleNormal(float scale)
    {
        if(m_pSprNormal != NULL)
        {
            if(m_pSprNormal->getScale() == m_scaleNormal)
            {
                m_pSprNormal->setScale(scale);
            }
        }
        m_scaleNormal = scale;
    }
    
    void UIButton::SetState(storm::UIButton::State state)
    {
        if(state == STATE_NORMAL)
        {
            scaleNodes(1.0f);
            opacityNodes(255);
            if (NULL != m_pSprClicked) m_pSprClicked->setVisible(false);
            if (NULL != m_pSprNormal) m_pSprNormal->setVisible(true);
            if (NULL != m_pSprClicking) m_pSprClicking->setVisible(false);
            
        }
        else if(state == STATE_CLICKED)
        {
            scaleNodes(1.0f);
            opacityNodes(255);
            
            if (NULL != m_pSprClicked)
            {
                CCSprite *pSprTemp = m_pSprNormal;
                m_pSprNormal = m_pSprClicked;
                m_pSprClicked = pSprTemp;
                m_pSprClicked->setVisible(false);
            }
            if (NULL != m_pSprNormal) m_pSprNormal->setVisible(true);
            if (NULL != m_pSprClicking) m_pSprClicking->setVisible(false);
        }
        
        else if(state == STATE_CLICKING)
        {
            if(NULL != m_pSprClicked) m_pSprClicked->setVisible(false);
            scaleNodes(m_scaleFactor);
            opacityNodes(m_opacityFactor);
            if(m_pSprClicking != NULL && m_pSprNormal != NULL)
            {
                m_pSprNormal->setVisible(false);
                m_pSprClicking->setVisible(true);
            }
        }
    }
   
    void UIButton::SetSprite(cocos2d::CCSprite *&pSpr, const char *szName, bool bUseFrame)
    {
        if(NULL != pSpr)
        {
            pSpr->removeFromParentAndCleanup(true);
            RemoveTransformNode(pSpr);
        }
        pSpr = bUseFrame ? CCSprite::createWithSpriteFrameName(szName) : CCSprite::create(szName);
      
        addChild(pSpr);
        AddTransformNode(pSpr);
    }

}